﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace splitPng
{
	class splitMod
	{
		public static Rectangle[] getRects(Bitmap pic)
		{
			List<Rectangle> rects = new List<Rectangle>();
			Rectangle rect;
			bool[][] tColor = getColors(pic);
			Console.WriteLine(tColor.Length.ToString(), tColor[0].Length.ToString());

			for(int x = 0; x < pic.Width; x++)
			{
				for(int y = 0; y < pic.Height; y++)
				{
					if (tColor[x][y])
					{
						rect = getPixelRect(tColor, x, y);
						if(rect.Width > 2 && rect.Height > 2)
						{
							rects.Add(rect);
						}
					}
				}
			}
			getSubPics(pic, rects.ToArray());
			return rects.ToArray();
		}

		// 获取颜色是否为透明 
		public static bool[][] getColors(Bitmap pic)
		{
			bool[][] tColor = new bool[pic.Width][];
			Color C;
			for(int xPos = 0; xPos < pic.Width; xPos++)
			{
				tColor[xPos] = new bool[pic.Height];
				for(int yPos = 0; yPos < pic.Height; yPos++)
				{
					C = pic.GetPixel(xPos, yPos);
					if(C.A <= 2)
					{
						tColor[xPos][yPos] = false;
					}
					else
					{
						tColor[xPos][yPos] = true;
					}
				}
			}

			return tColor;
		}

		public static Rectangle getPixelRect(bool[][] tColor, int x, int y)
		{
			Rectangle Rect = new Rectangle(new Point(x, y), new Size(1, 1));   //创建所在区域  

			bool flag;

			do
			{
				flag = false;

				while (R_Exist(tColor, Rect)) { Rect.Width++; flag = true; }
				while (D_Exist(tColor, Rect)) { Rect.Height++; flag = true; }
				while (L_Exist(tColor, Rect)) { Rect.Width++; Rect.X--; flag = true; }
				while (U_Exist(tColor, Rect)) { Rect.Height++; Rect.Y--; flag = true; }
			}
			while (flag == true);

			clearRect(tColor, Rect);            //清空已选择的区域  

			Rect.Width++;
			Rect.Height++;

			return Rect;
		}

		//判断坐标处是否存在非透明像素值  
		public static bool Exist(bool[][] tColor, int x, int y)
		{
			if (x < 0 || y < 0 || x >= tColor.Length || y >= tColor[0].Length)
				return false;
			else
				return tColor[x][y];
		}

		//判定区域Rect右侧是否存在像素点  
		public static bool R_Exist(bool[][] Colors, Rectangle Rect)
		{
			if (Rect.Right >= Colors.Length || Rect.Left < 0)
			{
				return false;
			}
			for (int i = 0; i < Rect.Height; i++)
			{
				if (Exist(Colors, Rect.Right + 1, Rect.Top + i) && Exist(Colors, Rect.Right, Rect.Top + i))
				{
					return true;
				}
			}
			return false;
		}

		// 判定区域的下方是否存在像素点
		public static bool D_Exist(bool[][] Colors, Rectangle Rect)
		{
			if (Rect.Bottom >= Colors[0].Length || Rect.Top < 0)
			{
				return false;
			}
			for (int i = 0; i < Rect.Width; i++)
			{
				if (Exist(Colors, Rect.Left + i, Rect.Bottom + 1) && Exist(Colors, Rect.Left + i, Rect.Bottom))
				{
					return true;
				}
			}
			return false;
		}

		//判定区域的左方是否存在像素点
		public static bool L_Exist(bool[][] Colors, Rectangle Rect)
		{
			if (Rect.Right >= Colors.Length || Rect.Left < 0)
			{
				return false;
			}
			for (int i = 0; i < Rect.Height; i++)
			{
				if (Exist(Colors, Rect.Left - 1, Rect.Top + i) && Exist(Colors, Rect.Left, Rect.Top + i))
				{
					return true;
				}
			}
			return false;
		}

		// 判定区域的上方是否存在像素点
		public static bool U_Exist(bool[][] Colors, Rectangle Rect)
		{
			if (Rect.Bottom >= Colors[0].Length || Rect.Top < 0)
			{
				return false;
			}
			for (int i = 0; i < Rect.Width; i++)
			{
				if (Exist(Colors, Rect.Left + i, Rect.Top - 1) && Exist(Colors, Rect.Left + i, Rect.Top))
				{
					return true;
				}
			}
				
			return false;
		}

		//清空区域内的像素非透明标记  
		public static void clearRect(bool[][] Colors, Rectangle Rect)
		{
			for (int i = Rect.Left; i < Rect.Right; i++)
			{
				for (int j = Rect.Top; j < Rect.Bottom; j++)
				{
					Colors[i][j] = false;
				}
			}
				
		}

		//获取图像buildPic的所有子图区域的图像  
		public static Bitmap[] getSubPics(Image buildPic, Rectangle[] buildRects)
		{
			//创建建筑块图像  
			Bitmap[] buildTiles = new Bitmap[buildRects.Length];
			for (int i = 0; i < buildRects.Length; i++)
			{
				buildTiles[i] = GetRect(buildPic, buildRects[i]);     //获取建筑块的图像
				buildTiles[i].Save(@"C:\Users\Administrator\Desktop\unpack\assets8" + i + ".png");
			}

			return buildTiles;
		}

		/// <summary>  
		/// 从图像pic中截取区域Rect构建新的图像  
		/// </summary>  
		public static Bitmap GetRect(Image pic, Rectangle Rect)
		{
			//创建图像  
			Rectangle drawRect = new Rectangle(0, 0, Rect.Width, Rect.Height);  //绘制整块区域  
			Bitmap tmp = new Bitmap(drawRect.Width, drawRect.Height);           //按指定大小创建位图  

			//绘制  
			Graphics g = Graphics.FromImage(tmp);                   //从位图创建Graphics对象  
			g.Clear(Color.FromArgb(0, 0, 0, 0));                    //清空  
			g.DrawImage(pic, drawRect, Rect, GraphicsUnit.Pixel);   //从pic的给定区域进行绘制 

			return tmp;     //返回构建的新图像 
		}
	}
}
